package controlSoftware.DataIn;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;

import controlSoftware.DataProcess.SensorData;

/**
 * UNUSED: Html parser
 * @author PaulHuang
 *
 */
public class HtmlParser {
	private String ip = "";

	public HtmlParser(String ip) {
		this.ip = ip;
	}

	//test
	public void printHtmlSrc() {
		URL url = null;
		String reader = null;

		try {
			url = new URL("http://" + ip);

			BufferedReader in = new BufferedReader(new InputStreamReader(
					url.openStream()));

			while ((reader = in.readLine()) != null) {
				System.out.println(reader);
			}
			in.close();
		} catch (Exception e) {
			System.out.println("Can't connect to server: " + ip + "\n"
					+ e.getMessage());
		}
	}

	public SensorData getSensorData() {
		URL url = null;
		String dataIn = null;
		SensorData data = null;

		double[] dataArray = new double[6];
		int dataIndex = 0;

		try {
			url = new URL("http://" + ip);

			BufferedReader in = new BufferedReader(new InputStreamReader(
					url.openStream()));

			while ((dataIn = in.readLine()) != null) {
				dataArray[dataIndex] = Double.parseDouble(dataIn);
				dataIndex++;
			}
			in.close();
		} catch (Exception e) {
			System.out.println("Can't connect to server: " + ip + "\n"
					+ e.getMessage());
		}
		
		//Build sensor data object
		//TODO: match data order
		//data = new SensorData(dataArray[0],dataArray[1],
		///		dataArray[2], dataArray[3],
		//		dataArray[4],dataArray[5]);
		
		return data;
	}
}